package cn.tedu.controller;

import cn.tedu.base.response.JsonResult;
import cn.tedu.mapper.UserMapper;
import cn.tedu.pojo.dto.InserterDto;
import cn.tedu.pojo.entiry.User;
import cn.tedu.pojo.vo.UserVO1;
import cn.tedu.pojo.vo.UserVO2;
import com.sun.org.apache.bcel.internal.generic.NEW;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author Justin Parker
 * @Date 2024/8/8 0008 10:00
 * @Version 1.0
 */
@Slf4j
@Api(tags = "01.用户模块")
@RestController
@RequestMapping("/v1/users")
public class UserController {

    @Autowired(required = false)
    private UserMapper userMapper;

    @PostMapping("insert")
    @ApiOperation(value = "用户注册功能")

    public JsonResult insertUser(@RequestBody InserterDto inserterDto) {
        log.debug("InserterDto="+ inserterDto);
        User user = new User();
        BeanUtils.copyProperties(inserterDto, user);
        user.setCreated(new Date());
        userMapper.insertUser(user);
        return JsonResult.ok();
    }

    @GetMapping("userList")
    @ApiOperation(value = "查询所有用户信息")
    public JsonResult userList(){

        List<UserVO1> userVO1s = userMapper.userList();
        return JsonResult.ok(userVO1s);
    }

    @GetMapping("delete")
    @ApiOperation(value = "删除用户功能")
    @ApiImplicitParam(name = "id",value = "用户编号",required = true,dataType = "int")
    public JsonResult deleteUser(Integer id){
        log.debug("id="+id);
        userMapper.deleteUser(id);
        return JsonResult.ok();
    }

    @PostMapping("update")
    @ApiOperation(value = "更新用户功能")
    public JsonResult updateUser(User user){
        log.debug("user="+user);
        userMapper.updateUser(user);
        return JsonResult.ok();
    }

    /**
     * 查询指定用户的用户名、密码和昵称
     * @PathVariable注解：声明路径参数，在路径PATH中获取参数
     */
    @GetMapping("selectById/{uid}")
    @ApiOperation(value = "查询指定用户信息")
    @ApiImplicitParam(name = "uid",value = "用户编号",required = true,dataType = "int")
    public JsonResult selectById(@PathVariable Integer uid){
        log.debug("uid="+uid);
        UserVO2 userVO2 = userMapper.selectById(uid);
        return JsonResult.ok(userVO2);
    }

}
